import { deleteOne, doAdd, doUpdate, postRequest, getDetails } from "@/api/mains";
import { succMsg, failMsg, responseMsg, confirm } from "@/utils/MessageUtils";

export function changeEditable(option: any) {
  option.value.treeConfig.disabled = true;
  option.value.treeConfig.isShow = true;
}

export function disableForm(option: any) {
  option.value.treeConfig.disabled = true;
  option.value.treeConfig.isShow = false;
  option.value.formConfig.disabled = true;
}

export function enableForm(option: any) {
  option.value.treeConfig.disabled = false;
  option.value.treeConfig.isShow = true;
  option.value.treeConfig.editable = true;
  option.value.formConfig.disabled = false;
}

//默认显示菜单管理
export async function handleShowData(option: any, form: any, fnBeforeShow?: any, fnAfterShowMain?: any) {
  option.value.treeConfig.isMainLoading = false; // 使用请求统一的loading true;
  const respData: any = await getDetails(option.value.treeConfig.entityUrl, option.value.treeConfig.id);

  if (fnBeforeShow != null && typeof fnBeforeShow != "undefined") {
    form.value = fnBeforeShow(respData.data);
  } else {
    form.value = respData.data;
  }
  if (fnAfterShowMain != null && typeof fnAfterShowMain != "undefined") {
    fnAfterShowMain(respData.data);
  } else {
    option.value.treeConfig.isMainLoading = false;
  }
  //form.value = respData.data
  option.value.treeConfig.parentid = form.value.parentid;
  option.value.treeConfig.editable = false;
  option.value.treeConfig.isShow = false;
}

export function handleAdd(option: any, formRef: any, form: any) {
  const curNodeId = form.value.id;
  const curNodeName = form.value.nodename;
  if (curNodeId == null || curNodeId == "") {
    failMsg("请选择节点");
    return;
  }
  //formRef.value.resetFields()
  form.value = {};
  option.value.formConfig.disabled = false;
  form.value.id = "";
  form.value.parentid = curNodeId;
  form.value.parentname = curNodeName;
  enableForm(option);
}

//控制编辑状态
export function handleEdit(option: any, form: any) {
  const curNodeId = form.value.id;
  if (curNodeId == null || curNodeId == "") {
    failMsg("请选择节点");
    return;
  }
  option.value.formConfig.disabled = false;
  enableForm(option);
}

//将当前点击的节点删除
export async function handleDelete(option: any, form: any, leftTreeRef: any) {
  const curNodeId = form.value.id;
  if (curNodeId == null || curNodeId == "") {
    failMsg("请选择节点");
    return;
  }
  confirm("你确定要删除选中项吗?", async () => {
    const respData = await deleteOne(option.value.treeConfig.entityUrl, option.value.treeConfig.id);
    responseMsg(respData, async () => {
      option.value.treeConfig.id = option.value.treeConfig.parentid; //删除后显示上级
      await leftTreeRef.value.handleRefreshTree(option.value.treeConfig.id);
    });
  });
}

export async function handleCancel(option: any, formRef: any, form: any, fnBeforeShow?: any) {
  formRef.value.resetFields();
  await handleShowData(option, form, fnBeforeShow);
  disableForm(option);
}

//保存
export async function handleSave(option: any, formRef: any, form: any, leftTreeRef: any, fnBeforeSave?: any) {
  //formRef.value.validate(async (valid: any) => {
  //if (valid) {
  option.value.treeConfig.isMainLoading = false; // 使用请求统一的loading true;
  if (form.value.id == "") {
    let newForm = form.value;
    if (fnBeforeSave != null && typeof fnBeforeSave != "undefined") {
      newForm = fnBeforeSave(form.value);
    }
    let respData: any = null;
    if (option.value.treeConfig.saveUrl) {
      respData = await postRequest(option.value.treeConfig.saveUrl, newForm);
    } else {
      respData = await doAdd(option.value.treeConfig.entityUrl, newForm);
    }
    option.value.treeConfig.isMainLoading = false;
    if (respData.code == 200) {
      option.value.treeConfig.id = respData.data.id;
      option.value.treeConfig.parentid = respData.data.parentid;
      option.value.treeConfig.editable = false;
      option.value.formConfig.disabled = true;
      await leftTreeRef.value.handleRefreshTree(option.value.treeConfig.id);
      succMsg(respData.msg);
      disableForm(option);
    } else {
      //failMsg(respData.msg);
    }
  } else {
    let newForm = form.value;
    if (fnBeforeSave != null && typeof fnBeforeSave != "undefined") {
      newForm = fnBeforeSave(form.value);
    }
    let respData: any = null;
    if (option.value.treeConfig.saveUrl) {
      respData = await postRequest(option.value.treeConfig.saveUrl, newForm);
    } else {
      respData = await doUpdate(option.value.treeConfig.entityUrl, newForm);
    }
    option.value.treeConfig.isMainLoading = false;
    if (respData.code == 200) {
      option.value.treeConfig.id = respData.data.id;
      option.value.treeConfig.parentid = respData.data.parentid;
      option.value.treeConfig.editable = false;
      option.value.formConfig.disabled = true;
      await leftTreeRef.value.handleRefreshTree(option.value.treeConfig.id);
      succMsg(respData.msg);
      disableForm(option);
    } else {
      //failMsg(respData.msg);
    }
  }
  //}
  //})
}

export function handleNodeClick(option: any, treeNodeId: any, form: any, fnBeforeShow?: any, fnAfterShowMain?: any) {
  if (option.value.treeConfig.editable) {
    //failMsg('编辑状态不能切换', 'error')
    //return
    changeEditable(option);
  }
  option.value.formConfig.disabled = true;
  option.value.treeConfig.id = treeNodeId;
  handleShowData(option, form, fnBeforeShow, fnAfterShowMain);
}

//刷新
export async function handleRefreshTree(option: any, leftTreeRef: any) {
  await leftTreeRef.value.handleRefreshTree(option.value.treeConfig.id);
}
